/**
 * Created by win 10 on 1/31/2018.
 */
function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}
$(function () {
    var id = GetQueryString("id");
    if (!$.cookie('FUND_USER_TOKEN')) {
        var d = new Date();
        d = d.getTime();
        $(".code-number").attr("src", '/sichuansfc/fund/user/imgcode?'+d);
    }
    $(".nav-list").children("li").hover(function () {
        $(this).find("ul").css("display", "block");
    },function () {
        $(this).find("ul").css("display", "none");
    })
    $(".second-nav").hover(function () {
        $(this).css("display", "block");
    },function () {
        $(this).css("display", "none");
    })
    /* 拉取我的基金管理人信息列表和基金信息列表 */
    $.ajax({
        url: '/sichuansfc/fund/manager/profile.bs',
        type: 'get',
        headers: {'FUND_USER_TOKEN': $.cookie('FUND_USER_TOKEN')},
        success: function (data) {
            if (data.status === 100000) {
                for (var i = 0; i < data.data.length; i++) {
                    if ($(".fund-manager")) {
                        $(".fund-manager").append(
                            '<option value="' + data.data[i].id + '">' + data.data[i].managerName + '</option>'
                        )
                    }
                    if ($(".admin-all")) {
                        $(".admin-all").append(
                            '<li><a href="admin-success.html?id='+data.data[i].id+'">' + data.data[i].managerName + '</a></li>'
                        )
                        for (var j = 0; j < data.data[i].funds.length; j++) {
                            $(".fund-all").append(
                                '<li><a href="fund-success.html?id='+data.data[i].funds[j].id+'">' + data.data[i].funds[j].fundName + '</a></li>'
                            )
                        }
                    }
                }
                if (location.href.indexOf('admin-success') > -1) {
                    for (var i = 0; i < $(".admin-all > li").length; i++) {
                        console.log($(this));
                        if (id === $($(".admin-all > li")[i]).find("a").attr("href").split('?')[1].split("=")[1]) {
                            $($(".admin-all > li")[i]).find("a").addClass('on-active-nav');
                        }
                    }
                }
                if (location.href.indexOf('fund-success') > -1) {
                    for (var i = 0; i < $(".fund-all > li").length; i++) {
                        console.log($(this));
                        if (id === $($(".fund-all > li")[i]).find("a").attr("href").split('?')[1].split("=")[1]) {
                            $($(".fund-all > li")[i]).find("a").addClass('on-active-nav');
                        }
                    }
                }
            }
        }
    })
    /* 判断是否有二级菜单 */
    for (var z = 0; z < $(".item-link").length; z++){
        if ($($(".item-link")[z]).find(".hide-nav").length > 0) {
            $($(".item-link")[z]).addClass("more-icon");
        }
    }
    /* 显示二级页面 */
    $(".item-link.more-icon").click(function () {
        if ($(this).children(".hide-nav").is(':hidden')) {
            $(this).children(".hide-nav").css("display", "block");
            $(this).find(".little-icon").attr("src", "../img/index/selected-show.png");
        } else {
            $(this).children(".hide-nav").css("display", "none");
            $(this).find(".little-icon").attr("src", "../img/index/not-selected.png");
        }
    })
    $(".hide-nav").click(function (e) {
        e = window.event || event;
        if (e.stopPropagation()) {
            e.stopPropagation();
        } else {
            e.cancelBubble = true;
        }
    })
    /* hover显示二级nav */
    $(".welcome-you > a").hover(function () {
        $(".personal-operate").css("display","block");
    },function () {
        $(".personal-operate").css("display","none");
    })
    $(".personal-operate").hover(function () {
        $(".personal-operate").css("display","block");
    },function () {
        $(".personal-operate").css("display","none");
    })

    /* 是否登录 */
    judgeLogin();
    /* 是否记住账号 */
    if (window.localStorage.getItem("userAccount")) {
        $("#account").val(window.localStorage.getItem("userAccount"));
    }
    /*（若没有登陆）禁止跳转*/
    $("a").click(function (e) {
        if (!$.cookie('FUND_USER_TOKEN')) {
            e = window.event || event;
            e.preventDefault();
            $(".bg-model").css("display", "block");
            $(".model-container").css("display", "block");
            return false;
        }
    })
    /*（若没有登陆）开始登录*/
    $(".start-login").click(function () {
        $(".bg-model").css("display", "block");
        $(".model-container").css("display", "block");
        var d = new Date();
        d = d.getTime();
        $(".code-number").attr("src", '/sichuansfc/fund/user/imgcode?'+d);
    })
    $(".code-number").click(function () {
        var d = new Date();
        d = d.getTime();
        $(this).attr('src', '/sichuansfc/fund/user/imgcode?' + d);
    })
    $(".close-model").click(function () {
        $(".bg-model").css("display", "none");
        $(".model-container").css("display", "none");
    })
    /*验证登录*/
    $(".start-travel").click(function (e) {
        e = window.event || event;
        e.preventDefault();
        var account = $("#account").val();
        var code = $("#code").val();
        var password = $("#password").val();
        $.ajax({
            url:'/sichuansfc/fund/manager/login?phone='+account+'&password='+password +'&code='+code,
            type: 'get',
            success: function (data) {
                if (data.status === 100000) {
                    // 服务端已将cookie存入castingyou.cn域名下 （线上代码需注释下一行代码）
                    $.cookie("FUND_USER_TOKEN", data.data.token, {expires: 7});
                    if (window.localStorage) {
                        window.localStorage.setItem('userName', data.data.phone);
                    }
                    if ($(".remenber-me").is(":checked")) {
                        if (window.localStorage) {
                            window.localStorage.setItem('userAccount', data.data.phone);
                        }
                    }
                    location.reload();
                } else {
                    $(".little-error").text('*' + data.msg);
                }
            },
            error: function () {
                console.log("服务器错误")
            }
        })
    })
    /* 退出登录 */
    $(".operation-login").click(function () {
        logOut();
    })
    $(".drop-out").click(function () {
        logOut();
    })
    /* 除开首页的强制登陆 */
    if (location.href.indexOf('index.html') === -1) {
        if (!$.cookie("FUND_USER_TOKEN")) {
            location.href = '../html/index.html'
            // $(".bg-model").css("display", "block");
            // $(".model-container").css("display", "block");
            // return false;
        }
    }
    if (!$.cookie("FUND_USER_TOKEN") && location.href.indexOf('?') > -1) {
        $(".bg-model").css("display", "block");
        $(".model-container").css("display", "block");
    }
})
function judgeLogin() {
    if ($.cookie("FUND_USER_TOKEN")) {
        $(".welcome-you > a").text('Hi, '+ window.localStorage.getItem('userName'))
        $(".login-out").css("display","none");
        $(".login-in").css("display","block");
    } else {
        $(".login-out").css("display","block");
        $(".login-in").css("display","none");
    }
}
function logOut() {
    $.cookie('FUND_USER_TOKEN', '', {expires: -1, path: '/'});
    $.cookie('FUND_USER_TOKEN', '', {expires: -1, path: '/fundSubmit/html'});
    $(".personal-operate").css("display","none");
    location.href = '../html/index.html'
}
